package com.xherorlclass.serialize.impl;

import com.xherorlclass.exceptions.SerializeException;
import com.xherorlclass.serialize.Serializer;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author ：Xherorl
 * @date ：Created in 2023/8/28 10:57
 * @description：
 */
@Slf4j
public class JdkSerializer implements Serializer {
    @Override
    public byte[] serialize(Object object) {
        if (object == null){
            return null;
        }
        // 将流的定义写在这里会自动关闭，不需要在写finally
        try(
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(baos);
        ){
            outputStream.writeObject(object);
            return baos.toByteArray();
        }catch (IOException e){
            log.error("序列化对象【{}】时发生异常。",object);
            throw new SerializeException(e);
        }
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> clazz) {
        if (bytes == null || clazz == null){
            return null;
        }
        try (
                // 将流的定义写在这里会自动关闭，不需要在写finally
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                ObjectInputStream objectInputStream = new ObjectInputStream(bais);
        ) {
            Object object = objectInputStream.readObject();
            if(log.isDebugEnabled()){
                log.debug("类【{}】已经完成了反序列化操作.",clazz);
            }
            return (T)object;
        } catch (IOException | ClassNotFoundException e) {
            log.error("反序列化对象【{}】时发生异常.",clazz);
            throw new SerializeException(e);
        }
    }
}
